<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('检修工作记录列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="col-sm-12 search-collapse">
            <form id="formId" class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">阳极编号：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="anodeCode" placeholder="请输入阳极编号"
                               class="layui-input" lay-required/>
                    </div>
<!--                    <label class="layui-form-label">检修类型：</label>-->
<!--                    <div class="layui-input-inline">-->
<!--                        <select name="fixType" lay-required dict-code="config_fix_type">-->
<!--                            <option value="">所有</option>-->
<!--                        </select>-->
<!--                    </div>-->
                    <label class="layui-form-label">是否报废：</label>
                    <div class="layui-input-inline">
                        <select name="isScrap" lay-required >
                            <option value="">所有</option>
                            <option value="1">报废</option>
                            <option value="0">未报废</option>
                        </select>
                    </div>
<!--                    <label class="layui-form-label">检修人员：</label>-->
<!--                    <div class="layui-input-inline">-->
<!--                        <select name="createBy">-->
<!--                            <option value="">所有</option>-->
<!--                            <option th:each="user:${users}" th:text="${user.realName}"-->
<!--                                    th:value="${user.userId}"></option>-->
<!--                        </select>-->
<!--                    </div>-->
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="fix-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="fix-table" lay-filter="fix-table"></table>
        <script type="text/html" id="fix-toolbar">
            <button sec:authorize="hasPermission('/work/fix/add','work:fix:add')"
                    class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/work/fix/remove','work:fix:remove')"
                    class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
        <script type="text/html" id="fix-bar">
            {{#if(d.fixType =='scrap'){ }}
            <button sec:authorize="hasPermission('/work/block/addFile','work:block:addFile')"
                    class="pear-btn pear-btn-primary pear-btn-sm" lay-event="upload"><i
                    class="layui-icon layui-icon-add-1"></i>
            </button>
            {{#} }}
            <button sec:authorize="hasPermission('/work/fix/edit','work:fix:edit')"
                    class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>
            </button>
            <button sec:authorize="hasPermission('/work/fix/remove','work:fix:remove')"
                    class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                    class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
        <script id="fixType" type="text/html">
            {{#if(d.isScrap != '1' ){ }}
            <span>未报废</span>
            {{# }else{ }}
            <span>报废</span>
            {{# } }}
        </script>
        <!--图片预览-->
        <script id="file-image" type="text/html">
            {{#if(d.content != null && d.content != ''){ }}
            <a href="javascript:void(0);" class="pear-text">查看</a>
            {{# } }}
        </script>
        <!--序号-->
        <script id="index" type="text/html">
            <span>{{d.LAY_TABLE_INDEX + 1}}</span>
        </script>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'dictionary'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let prefix = "/work/fix/";

        let cols = [
            [
                {type: 'checkbox'},
                {
                    field: 'index',
                    title: '序号',
                    templet: "#index"
                },
                {
                    field: 'id',
                    title: '标识'
                },
                {
                    field: 'anodeCode',
                    title: '阳极编号'
                },
                {
                    field: 'isScrap',
                    title: '是否报废',
                    templet: '#fixType'
                },
                // {
                //     field: 'content',
                //     title: '报废图片',
                // },
                {
                    field: 'show',
                    title: '图片详情',
                    templet: "#file-image",
                    event: "file-image"
                },
                {
                    field: 'createName',
                    title: '检修人员'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                },
                {title: '操作', toolbar: '#fix-bar', align: 'center', width: 150}
            ]
        ];

        table.render({
            elem: '#fix-table',
            url: prefix + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#fix-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            done: function (res, curr, count) {
                $(".layui-table-box").find("[data-field='id']").css("display", "none")
            }
        });

        table.on('tool(fix-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'upload') {
                window.upload(obj);
            } else {
                window.show(obj);
            }
        });

        table.on('toolbar(fix-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(fix-query)', function (data) {
            table.reload('fix-table', {where: data.field})
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增检修工作记录',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改检修工作记录',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
        }

        window.upload = function (obj) {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['100%', '100%'],
                content: prefix + 'file/add?id=' + obj.data['id']
            });
        }

        //预览多图
        window.show = function (obj) {
            debugger
            let imgArr = [];
            let content = obj.data.content;
            if (content != null && content != '') {
                let ids = JSON.parse(content).ids;
                for (let i = 0; i < ids.length; i++) {
                    let fileId = ids[i]; //图片id
                    let image = "/system/file/download/" + fileId;
                    let img = {};
                    img.src = image;
                    imgArr.push(img);
                }
                layer.photos({
                    photos: {
                        "title": "预览图片",
                        "start": 0,
                        "data": imgArr
                    }
                    , anim: 5
                });
                imgArr = []//清空图片列表
            }
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该检修工作记录', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些检修工作记录', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('fix-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('fix-table', {where: param});
        }
    })
</script>
</body>
</html>